<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="标题" prop="title">
            <el-input v-model="dataForm.title" placeholder="标题"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地区" prop="area">
            <el-input v-model="dataForm.area" placeholder="地区"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="公司名" prop="name">
            <el-input v-model="dataForm.name" placeholder="公司名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公司领域" prop="field">
            <el-input v-model="dataForm.field" placeholder="公司领域"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="工资" prop="wage">
            <el-input v-model="dataForm.wage" placeholder="工资"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作经验" prop="experience">
            <el-input v-model="dataForm.experience" placeholder="工作经验"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="规模" prop="scale">
            <el-input v-model="dataForm.scale" placeholder="规模"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="职位状态" prop="statu">
            <template>
              <el-radio v-model="dataForm.statu" :label="0">正常</el-radio>
              <el-radio v-model="dataForm.statu" :label="1">下架</el-radio>
            </template>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="录用人数" prop="num" disabled="">
            <el-input v-model="dataForm.num" placeholder="录用人数" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="总人数" prop="total">
            <el-input v-model="dataForm.total" placeholder="应聘总人数" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    <el-form-item label="福利" prop="welfare">
      <el-input v-model="dataForm.welfare" placeholder="福利"></el-input>
    </el-form-item>
    <el-form-item label="详情页" prop="detail">
      <el-input v-model="dataForm.detail" placeholder="详情页"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          area: '',
          name: '',
          field: '',
          wage: '',
          experience: '',
          scale: '',
          welfare: '',
          detail: '',
          num: 0,
          total: 0,
          statu: 0   // 默认为正常0
        },
        dataRule: {
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '地区不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '公司名不能为空', trigger: 'blur' }
          ],
          field: [
            { required: true, message: '公司领域不能为空', trigger: 'blur' }
          ],
          wage: [
            { required: true, message: '工资不能为空', trigger: 'blur' }
          ],
          experience: [
            { required: true, message: '工作经验不能为空', trigger: 'blur' }
          ],
          scale: [
            { required: true, message: '规模不能为空', trigger: 'blur' }
          ],
          welfare: [
            { required: true, message: '福利不能为空', trigger: 'blur' }
          ],
          detail: [
            { required: true, message: '详情页不能为空', trigger: 'blur' }
          ],
          statu: [
            { required: true, message: '职位状态0正常1下架不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/company/qycompany/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.qyCompany.title
                this.dataForm.area = data.qyCompany.area
                this.dataForm.name = data.qyCompany.name
                this.dataForm.field = data.qyCompany.field
                this.dataForm.wage = data.qyCompany.wage
                this.dataForm.experience = data.qyCompany.experience
                this.dataForm.scale = data.qyCompany.scale
                this.dataForm.welfare = data.qyCompany.welfare
                this.dataForm.detail = data.qyCompany.detail
                this.dataForm.num = data.qyCompany.num
                this.dataForm.total = data.qyCompany.total
                this.dataForm.statu = data.qyCompany.statu
                console.log(this.dataForm.statu)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/company/qycompany/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'area': this.dataForm.area,
                'name': this.dataForm.name,
                'field': this.dataForm.field,
                'wage': this.dataForm.wage,
                'experience': this.dataForm.experience,
                'scale': this.dataForm.scale,
                'welfare': this.dataForm.welfare,
                'detail': this.dataForm.detail,
                'num': this.dataForm.num,
                'total': this.dataForm.total,
                'statu': this.dataForm.statu
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
